package com.bigdata.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @className: RandomDate_java
 * @description: TODO 随机日期工具类
 * @author 石永鑫大数据工作室
 * @date: 2021/3/12 21:46
 * @version: 1.0
 */
public class RandomDate_java {
    Long logDateTime =0L;//
    int maxTimeStep=0 ;
    /**
     *
     * @param startDate
     * @param endDate
     * @param num
     */
    public RandomDate_java(Date startDate , Date endDate, int num) {
        Long avgStepTime = (endDate.getTime()- startDate.getTime())/num;
        this.maxTimeStep=avgStepTime.intValue()*2;
        this.logDateTime=startDate.getTime();
    }

    /**
     *
     * @return
     */
    public  Date  getRandomDate() {
        int  timeStep = new Random().nextInt(maxTimeStep);
        logDateTime = logDateTime+timeStep;
        return new Date( logDateTime);
    }

    @Override
    public String toString() {
        return "RandomDate{" +
                "logDateTime=" + logDateTime +
                ", maxTimeStep=" + maxTimeStep +
                '}';
    }

    public static void main(String[] args) throws ParseException {
        String startTimeString = "2020-12-01 12:12:12";
        String endTimeString = "2020-12-12 12:12:12";
        Date startTime = new SimpleDateFormat("yyyy-MM-dd").parse(startTimeString);
        Date endTime = new SimpleDateFormat("yyyy-MM-dd").parse(endTimeString);
        RandomDate_java randomDateJava = new RandomDate_java(startTime, endTime, 300000);
        System.out.println(randomDateJava); //RandomDate{logDateTime=1606752000000, maxTimeStep=6336}
    }
}


